/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.db.jpa;

import com.db.jpa.exceptions.IllegalOrphanException;
import com.db.jpa.exceptions.NonexistentEntityException;
import db.Labeltypes;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import db.Label;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author devashish
 */
public class LabeltypesJpaController {

    public LabeltypesJpaController() {
        emf = Persistence.createEntityManagerFactory("ExplorePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Labeltypes labeltypes) {
        if (labeltypes.getLabelCollection() == null) {
            labeltypes.setLabelCollection(new ArrayList<Label>());
        }
        if (labeltypes.getLabelCollection1() == null) {
            labeltypes.setLabelCollection1(new ArrayList<Label>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Label> attachedLabelCollection = new ArrayList<Label>();
            for (Label labelCollectionLabelToAttach : labeltypes.getLabelCollection()) {
                labelCollectionLabelToAttach = em.getReference(labelCollectionLabelToAttach.getClass(), labelCollectionLabelToAttach.getLabelId());
                attachedLabelCollection.add(labelCollectionLabelToAttach);
            }
            labeltypes.setLabelCollection(attachedLabelCollection);
            Collection<Label> attachedLabelCollection1 = new ArrayList<Label>();
            for (Label labelCollection1LabelToAttach : labeltypes.getLabelCollection1()) {
                labelCollection1LabelToAttach = em.getReference(labelCollection1LabelToAttach.getClass(), labelCollection1LabelToAttach.getLabelId());
                attachedLabelCollection1.add(labelCollection1LabelToAttach);
            }
            labeltypes.setLabelCollection1(attachedLabelCollection1);
            em.persist(labeltypes);
            for (Label labelCollectionLabel : labeltypes.getLabelCollection()) {
                Labeltypes oldLabelTypeIdOfLabelCollectionLabel = labelCollectionLabel.getLabelTypeId();
                labelCollectionLabel.setLabelTypeId(labeltypes);
                labelCollectionLabel = em.merge(labelCollectionLabel);
                if (oldLabelTypeIdOfLabelCollectionLabel != null) {
                    oldLabelTypeIdOfLabelCollectionLabel.getLabelCollection().remove(labelCollectionLabel);
                    oldLabelTypeIdOfLabelCollectionLabel = em.merge(oldLabelTypeIdOfLabelCollectionLabel);
                }
            }
            for (Label labelCollection1Label : labeltypes.getLabelCollection1()) {
                Labeltypes oldLabelTypeId1OfLabelCollection1Label = labelCollection1Label.getLabelTypeId1();
                labelCollection1Label.setLabelTypeId1(labeltypes);
                labelCollection1Label = em.merge(labelCollection1Label);
                if (oldLabelTypeId1OfLabelCollection1Label != null) {
                    oldLabelTypeId1OfLabelCollection1Label.getLabelCollection1().remove(labelCollection1Label);
                    oldLabelTypeId1OfLabelCollection1Label = em.merge(oldLabelTypeId1OfLabelCollection1Label);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Labeltypes labeltypes) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Labeltypes persistentLabeltypes = em.find(Labeltypes.class, labeltypes.getLabelTypeId());
            Collection<Label> labelCollectionOld = persistentLabeltypes.getLabelCollection();
            Collection<Label> labelCollectionNew = labeltypes.getLabelCollection();
            Collection<Label> labelCollection1Old = persistentLabeltypes.getLabelCollection1();
            Collection<Label> labelCollection1New = labeltypes.getLabelCollection1();
            List<String> illegalOrphanMessages = null;
            for (Label labelCollectionOldLabel : labelCollectionOld) {
                if (!labelCollectionNew.contains(labelCollectionOldLabel)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Label " + labelCollectionOldLabel + " since its labelTypeId field is not nullable.");
                }
            }
            for (Label labelCollection1OldLabel : labelCollection1Old) {
                if (!labelCollection1New.contains(labelCollection1OldLabel)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Label " + labelCollection1OldLabel + " since its labelTypeId1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Label> attachedLabelCollectionNew = new ArrayList<Label>();
            for (Label labelCollectionNewLabelToAttach : labelCollectionNew) {
                labelCollectionNewLabelToAttach = em.getReference(labelCollectionNewLabelToAttach.getClass(), labelCollectionNewLabelToAttach.getLabelId());
                attachedLabelCollectionNew.add(labelCollectionNewLabelToAttach);
            }
            labelCollectionNew = attachedLabelCollectionNew;
            labeltypes.setLabelCollection(labelCollectionNew);
            Collection<Label> attachedLabelCollection1New = new ArrayList<Label>();
            for (Label labelCollection1NewLabelToAttach : labelCollection1New) {
                labelCollection1NewLabelToAttach = em.getReference(labelCollection1NewLabelToAttach.getClass(), labelCollection1NewLabelToAttach.getLabelId());
                attachedLabelCollection1New.add(labelCollection1NewLabelToAttach);
            }
            labelCollection1New = attachedLabelCollection1New;
            labeltypes.setLabelCollection1(labelCollection1New);
            labeltypes = em.merge(labeltypes);
            for (Label labelCollectionNewLabel : labelCollectionNew) {
                if (!labelCollectionOld.contains(labelCollectionNewLabel)) {
                    Labeltypes oldLabelTypeIdOfLabelCollectionNewLabel = labelCollectionNewLabel.getLabelTypeId();
                    labelCollectionNewLabel.setLabelTypeId(labeltypes);
                    labelCollectionNewLabel = em.merge(labelCollectionNewLabel);
                    if (oldLabelTypeIdOfLabelCollectionNewLabel != null && !oldLabelTypeIdOfLabelCollectionNewLabel.equals(labeltypes)) {
                        oldLabelTypeIdOfLabelCollectionNewLabel.getLabelCollection().remove(labelCollectionNewLabel);
                        oldLabelTypeIdOfLabelCollectionNewLabel = em.merge(oldLabelTypeIdOfLabelCollectionNewLabel);
                    }
                }
            }
            for (Label labelCollection1NewLabel : labelCollection1New) {
                if (!labelCollection1Old.contains(labelCollection1NewLabel)) {
                    Labeltypes oldLabelTypeId1OfLabelCollection1NewLabel = labelCollection1NewLabel.getLabelTypeId1();
                    labelCollection1NewLabel.setLabelTypeId1(labeltypes);
                    labelCollection1NewLabel = em.merge(labelCollection1NewLabel);
                    if (oldLabelTypeId1OfLabelCollection1NewLabel != null && !oldLabelTypeId1OfLabelCollection1NewLabel.equals(labeltypes)) {
                        oldLabelTypeId1OfLabelCollection1NewLabel.getLabelCollection1().remove(labelCollection1NewLabel);
                        oldLabelTypeId1OfLabelCollection1NewLabel = em.merge(oldLabelTypeId1OfLabelCollection1NewLabel);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Short id = labeltypes.getLabelTypeId();
                if (findLabeltypes(id) == null) {
                    throw new NonexistentEntityException("The labeltypes with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Short id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Labeltypes labeltypes;
            try {
                labeltypes = em.getReference(Labeltypes.class, id);
                labeltypes.getLabelTypeId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The labeltypes with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Label> labelCollectionOrphanCheck = labeltypes.getLabelCollection();
            for (Label labelCollectionOrphanCheckLabel : labelCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Labeltypes (" + labeltypes + ") cannot be destroyed since the Label " + labelCollectionOrphanCheckLabel + " in its labelCollection field has a non-nullable labelTypeId field.");
            }
            Collection<Label> labelCollection1OrphanCheck = labeltypes.getLabelCollection1();
            for (Label labelCollection1OrphanCheckLabel : labelCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Labeltypes (" + labeltypes + ") cannot be destroyed since the Label " + labelCollection1OrphanCheckLabel + " in its labelCollection1 field has a non-nullable labelTypeId1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(labeltypes);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Labeltypes> findLabeltypesEntities() {
        return findLabeltypesEntities(true, -1, -1);
    }

    public List<Labeltypes> findLabeltypesEntities(int maxResults, int firstResult) {
        return findLabeltypesEntities(false, maxResults, firstResult);
    }

    private List<Labeltypes> findLabeltypesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Labeltypes as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Labeltypes findLabeltypes(Short id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Labeltypes.class, id);
        } finally {
            em.close();
        }
    }

    public int getLabeltypesCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Labeltypes as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
